<?php
error_reporting(E_ALL ^E_NOTICE);
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPayProsessStart', 'jeProcessEpayStart' );
$_MAMBOTS->registerFunction( 'onPayProsessFinish', 'jeProcessEpayFinish' );

function jeProcessEpayFinish()
{
    $processor = mosGetParam( $_REQUEST, 'processor'); // success or fail
    
    if($processor != '.epay') return false;

    $order['id'] = mosGetParam( $_REQUEST, 'orderid');
    $order['pay'] = mosGetParam( $_REQUEST, 'pay');
    $order['gateway'] = $processor;
    $order['gateway_id'] = mosGetParam( $_REQUEST, '...');
    $order['price'] = mosGetParam( $_REQUEST, 'amount');
    
    return $order;
}

function jeProcessEpayStart( $params )
{
    Global $my, $database;
    
    if($params['processor'] != '.epay') return;
    
    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$params[processor]}'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
  
    $param['amount']        = floatval($params['order_amount']);
    $param['option'] = mosGetParam( $_REQUEST, 'option');
    $param['task']   = 'pay';
    $param['order_id'] = $params['order_id'];
    $param['processor'] = $params['processor'];

    global $mosConfig_live_site;
    
    $return        = $mosConfig_live_site.'/index.php?'.jpArray2Url($param).'&pay=success';
    $cancel_return = $mosConfig_live_site.'/index.php?'.jpArray2Url($param).'&pay=fail';
    
    $param['accepturl']  = $return;
    $param['declineurl'] = $cancel_return;
    
    //echo $param['accepturl']."<BR>";
    //echo $param['declineurl']."<BR>";
    //return;
    
    if(!$botParams->def( 'language') || !$botParams->def( 'currency') || !$botParams->def( 'merchantnumber') || !$param['amount'])
    {
        echo "<P>Error Payment Processing: some of the params passed are empty</P>";
        return;
    }
    
    echo '<script language="javascript" src="http://www.epay.dk/js/standardwindow.js"></script>';
    echo '<script language="javascript">function checkData(){';
    echo 'var objAmount = document.ePay.useramount;';
		echo '		var re = /,/g;';
		echo '		var result = objAmount.value.replace(re, ".");';
		echo '		objAmount.value = result;';
		echo '		if(isNaN(objAmount.value) || objAmount.value.length < 1)';
		echo '		{';
		echo '			alert("Enter amount!");';
		echo '			objAmount.focus();';
		echo '			return false;';
		echo '		}';
		echo '		if(document.ePay.orderid.value.length < 1)';
		echo '		{';
		echo '			alert("Enter orderID!");';
		echo '			document.ePay.orderid.focus();';
		echo '			return false;';
		echo '		}';
		echo '		var amount = parseFloat(objAmount.value) * 100;';
		echo '		document.ePay.amount.value = amount;';
    echo '    open_ePay_window();}';
    echo ' </script>';

    echo "<form action=\"https://ssl.ditonlinebetalingssystem.dk/popup/default.asp\" method=\"post\" name=\"ePay\" target=\"ePay_window\" id=\"ePay\">";
    if($botParams->def( 'key')) 
        echo "<input type=\"hidden\" name=\"md5key\" value=\"".
            MD5( $botParams->def( 'currency').$params['order_amount'].
            $params['order_id'].$botParams->def( 'key'))."\"/>";

    echo "
	    <input type=\"hidden\" name=\"orderid\"       value=\"".$params['order_id']."\"/>
	    <input type=\"hidden\" name=\"language\"      value=\"".$botParams->def( 'language')."\"/>
      <input type=\"hidden\" name=\"useramount\"    value=\"".floatval($params['order_amount'])."\"/>
      <input type=\"hidden\" name=\"description\"   value=\"".strval($params['order_title'])."\"/>
      <input type=\"hidden\" name=\"amount\"        value=\"\"/>
      <input type=\"hidden\" name=\"merchantnumber\" value=\"".$botParams->def( 'merchantnumber')."\">
      <input type=\"hidden\" name=\"currency\"      value=\"".$botParams->def( 'currency')."\">
      <input type=\"hidden\" name=\"ordretext\"     value=\"".$botParams->def( 'ordertext')."\"/>
      <input type=\"hidden\" name=\"accepturl\"     value=\"$return\"/>
      <input type=\"hidden\" name=\"declineurl\"    value=\"$cancel_return\"/>
      <input type=\"hidden\" name=\"group\"         value=\"\">
      <input type=\"hidden\" name=\"authmail\"      value=\"".$botParams->def( 'email')."\">
      <input type=\"hidden\" name=\"instantcapture\" value=\"".$botParams->def( 'instantcapture')."\"/> 
      <input type=\"hidden\" name=\"windowstate\"   value=\"".$botParams->def( 'windowstate')."\">
      <!--<h3>Invioce is ready, please, <a href=\"#\" onclick=\"checkData()\">Open Pyment Window</a></h3>-->
      </form> 
      <script language=\"JavaScript\" type=\"text/javascript\">
      <!--
      checkData();
      //-->
      </script>
    ";
    //$url = 'https://ssl.ditonlinebetalingssystem.dk/popup/default.asp?'.jpArray2Url($param);
    //echo $url;
    //header("location: $url");
}

if(!defined('JP_IMPLODE'))
{
    define('JP_IMPLODE', 1);
    function jpArray2Url($param)
    {
        foreach($param AS $k => $v)
        {
            $out[] = "$k=$v";
        }
        return implode('&', $out );
    }
}



?>
